library AmmoUsage initializer init requires LegacySystem, AmmoAPI

	private function AmmoUsage takes nothing returns boolean
		local unit u = GetAttacker()   // get who fires
		local unit target = GetTriggerUnit() // get who gets shot
		local player p = GetOwningPlayer(u)
		local integer i = GetConvertedPlayerId(p)

		local integer id = GetUnitTypeId(u) // get who fires (id)
		local real energy
	  
		// anti attack click
		//if IsUnitAlly(target, Player(11)) then
		//	 if IsUnitAlly(u, Player(11)) then
		//		call IssueImmediateOrder(u,"stop")
		//		set u = null
		//		set target = null
		//		return 
		////	 endif
		//endif

		// sniper or mm cant attack cliff-blocked areas
		//if(id == 'H00S' or id == 'H003' ) then
		//	if(visible(GetUnitX(u), GetUnitY(u), GetUnitX(target), GetUnitY(target), 100.0)==false) then
		//		call IssueImmediateOrder(u,"holdposition")
		//		set target = null
		//		set u = null
		//		return false
		//	endif
		//endif     

		if GetUnitPointValue(u)!=10 or udg_Reloading[i]!=0 then
			set u = null
			return false
		endif
		
		if AmmoSystemRounds[i]<=0 and AmmoSystemMagazines[i]<=0 and id!='h00E' then
			set udg_Reloading[i]=3
			call UnitAddAbility(u,'A02H')
		endif

		if id=='H000' or id=='h00E' then
			set energy = GetUnitState(u,UNIT_STATE_MANA)
			if energy>1.0 then
				call SetUnitState(u,UNIT_STATE_MANA,energy-udg_FuelEfficiency[i])
				if id=='H000' then
					set AmmoSystemRounds[i] = AmmoSystemRounds[i] - 1
					call SetPlayerState(p,PLAYER_STATE_RESOURCE_GOLD,AmmoSystemRounds[i])
				endif
			else
				call IssueImmediateOrder(u,"holdposition")
			endif
		else
			set AmmoSystemRounds[i] = AmmoSystemRounds[i] - 1
			call SetPlayerState(p,PLAYER_STATE_RESOURCE_GOLD,AmmoSystemRounds[i])
		endif
		
		set target = null
		set u = null
		set p = null
		return false
	endfunction

	
	private function MiniMarineAmmunitionConditionTriggerUnit takes nothing returns boolean
		return GetUnitPointValue(GetTriggerUnit()) == 12
	endfunction
	
	private function MiniMarineAmmunitionConditionAttacker takes nothing returns boolean
		return GetUnitPointValue(GetAttacker()) == 12
	endfunction
	
    private function MiniMarineAmmunition takes nothing returns nothing
        local unit u = null
        local string us
        local real m
        local integer current = udg_Second
		
        if GetTriggerEventId()==EVENT_PLAYER_UNIT_SPELL_CAST then
            if GetSpellAbilityId()=='A00D' and GetLoadState(I2S(GetHandleId(GetTriggerUnit())))==1 then //Reload ID
                set u = GetTriggerUnit()
                call SetUnitState(u,UNIT_STATE_MANA,0)
            else
                return
            endif
        elseif GetUnitPointValue(GetAttacker())==12 then
            set u = GetAttacker()
        else
            return
        endif
		
        set us = I2S(GetHandleId(u))
		if GetLoadState(us)==2 then
            call IssueImmediateOrder(u,"holdposition")
		else
			set m = GetUnitState(u,UNIT_STATE_MANA)
            if m<=0.9 and GetUnitTypeId(u)!='z009' then
                if GetRandomInt(0,udg_ReloadText)==1 then
                    call FieldChat(u,5.0, "|cffcd950c" + GetUnitName(u) + "|r : Reload!" )
                endif
                call SetLoadState(us,2)
                call UnitAddAbility(u,'A02H')
                if current-udg_ClipOutLimit!=0 then
                    set bj_meleeNearestMineDist = GetUnitX(u)
                    set bj_lastTransmissionDuration = GetUnitY(u)
                    set udg_soundpath = "war3mapImported\\clipout1.wav"
                    call PlaySoundPath3D.execute()
                else
                    set udg_ClipOutLimit=current
                endif
                call PolledWait(6.5)
                if current-udg_ClipInLimit!=0 then
                    set bj_meleeNearestMineDist = GetUnitX(u)
                    set bj_lastTransmissionDuration = GetUnitY(u)
                    set udg_soundpath = "war3mapImported\\clipin1.wav"
                    call PlaySoundPath3D.execute()
                else
                    set udg_ClipInLimit=current
                endif
				call SetLoadState(us,1)
                if GetLoadState(us)!=0 then
                    call UnitRemoveAbility(u,'A02H')
                    call SetUnitState(u,UNIT_STATE_MANA,GetUnitState(u,UNIT_STATE_MAX_MANA))
                endif
            elseif GetUnitTypeId(u)=='z009' and m<=0.9 then
                call IssueImmediateOrder(u,"holdposition")
            else
                call SetUnitState(u,UNIT_STATE_MANA,m-1.0)
            endif
        endif

        set u = null
    endfunction
   
	
	private function init takes nothing returns nothing
		local trigger t
		set t = CreateTrigger()
		call TriggerRegisterAnyUnitEventBJ(t,EVENT_PLAYER_UNIT_ATTACKED)   
		call TriggerAddCondition(t, Condition (function AmmoUsage))
		
		set t =  CreateTrigger()
		call TriggerRegisterAnyUnitEventBJ(t,EVENT_PLAYER_UNIT_SPELL_CAST)
		call TriggerAddCondition(t, Condition (function MiniMarineAmmunitionConditionTriggerUnit))
		call TriggerAddAction( t, function MiniMarineAmmunition )
		
		set t =  CreateTrigger()
        call TriggerRegisterAnyUnitEventBJ(t,EVENT_PLAYER_UNIT_ATTACKED)
		call TriggerAddCondition(t, Condition (function MiniMarineAmmunitionConditionAttacker))
        call TriggerAddAction( t, function MiniMarineAmmunition )
        set t = null
	endfunction
endlibrary